package org.example.admin.manage.service.impl;

import org.example.admin.manage.repository.SysAdminRepository;
import org.example.admin.manage.service.SysAdminService;
import org.example.commons.entity.SysAdmin;
import org.example.commons.entity.SysUser;
import org.example.commons.utils.UpdateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * author 陈伯瑞
 * date 2020/5/25 0025 17:33
 * version 1.0
 **/
@Service
public class SysAdminServiceImpl implements SysAdminService {
    @Autowired
    SysAdminRepository sysAdminRepository;

    @Override
    public SysAdmin queryById(Long id) {
        return sysAdminRepository.findById(id).orElse(null);
    }

    @Override
    public Page<SysAdmin> queryAllPage(Pageable pageable) {
        return sysAdminRepository.findAll(pageable);
    }

    @Override
    public SysAdmin insert(SysAdmin entity) {
        entity = updateInfo(entity);
        return sysAdminRepository.save(entity);
    }

    @Override
    public SysAdmin update(SysAdmin entity) {
        entity = updateInfo(entity);
        return sysAdminRepository.save(entity);
    }

    @Override
    public void deleteById(Long id) {
       sysAdminRepository.deleteById(id);
    }

    @Override
    public List<SysAdmin> findByAdminIdIn(Long[] ids) {
        return sysAdminRepository.findByAdminIdIn(ids);
    }

    @Override
    public List<SysAdmin> queryAll() {
        return sysAdminRepository.findAll();
    }

    @Override
    public SysAdmin findByOpenid(String openid) {
        return sysAdminRepository.findByOpenid(openid);
    }

    @Override
    public SysAdmin findByPhone(String phone) {
        return sysAdminRepository.findByPhone(phone);
    }

    @Override
    public SysAdmin findByName(String name) {
        return sysAdminRepository.findByName(name);
    }

    @Override
    @Transactional
    public void deleteByAdminIdIn(Long[] ids) {
        sysAdminRepository.deleteByAdminIdIn(ids);
    }

    @Override
    public Page<SysAdmin> searchList(String name, String phone, Integer status, Date[] dates, Pageable pageable) {
        return sysAdminRepository.searchList("%"+name+"%","%"+phone+"%",status,dates,pageable);
    }


    @Override
    public String getPassword(Long id) {
        return sysAdminRepository.getPassword(id);
    }

    @Override
    public void setPassword(Long id, String password) {
        sysAdminRepository.setPassword(id,password);
    }

    /**
     * 补全信息 (null表示未更新，要替换成原来的数据)
     * @param admin 管理员
     * @return
     */
    private SysAdmin updateInfo(SysAdmin admin){
        if (admin==null)  return null;
        if (admin.getAdminId()==null) return admin;
        SysAdmin sysAdmin = queryById(admin.getAdminId());
        if (sysAdmin==null) return null;
        //把非空属性更新
        UpdateUtil.copyNullProperties(admin,sysAdmin);
        return sysAdmin;
    }
}
